package com.hushan.elevator.service.impl;

import com.hushan.elevator.dao.DeviceInfoRepository;
import com.hushan.elevator.dao.RiskRecordRepository;
import com.hushan.elevator.model.Department;
import com.hushan.elevator.model.DeviceInfo;
import com.hushan.elevator.model.RiskRecord;
import com.hushan.elevator.model.User;
import com.hushan.elevator.security.SecurityUtils;
import com.hushan.elevator.service.*;
import com.hushan.elevator.vo.MonitorDtree;
import com.hushan.elevator.vo.StatisticsVo;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StaticsServiceImpl implements StaticsService {
    private final DeptService deptService;
    private final DeviceInfoService deviceInfoService;
    private final RiskRecordRepository riskRecordDao;
    private final RiskRecordService riskRecordService;
    private final MntPersonService mntPersonService;
    private final UserService userService;
    private final DeviceInfoRepository deviceInfoDao;

    public StaticsServiceImpl(DeptService deptService, DeviceInfoService deviceInfoService, RiskRecordRepository riskRecordDao, RiskRecordService riskRecordService, MntPersonService mntPersonService, UserService userService, DeviceInfoRepository deviceInfoDao) {
        this.deptService = deptService;
        this.deviceInfoService = deviceInfoService;
        this.riskRecordDao = riskRecordDao;
        this.riskRecordService = riskRecordService;
        this.mntPersonService = mntPersonService;
        this.userService = userService;
        this.deviceInfoDao = deviceInfoDao;
    }

    @Override
    public StatisticsVo getHomeStatics() {
        User requester = SecurityUtils.getUser();
        StatisticsVo statisticsVo = new StatisticsVo();
        Department dept = deptService.findDeptByUserId(requester.getUserId());
        List<Department> childDept = deptService.findChildByDeptId(dept.getDeptId());
        childDept.add(dept);

        int deviceCount = 0;
        int online = 0;
        int offline = 0;
        int riskCount = 0;
        int mntCount = 0;
        int deptCount = childDept.size();
        int userCount = 0;

        for (Department item : childDept) {
            deviceCount += deviceInfoService.findCountByDept(item.getDeptId());
            offline += deviceInfoService.findOnlineCountByDept(false, item.getDeptId());
            riskCount += riskRecordService.countByDept(item.getDeptId());
            mntCount += mntPersonService.findCountByDept(item.getDeptId());
            userCount += userService.findUserCountByDept(item.getDeptId());
        }
        online += deviceCount-offline;
        statisticsVo.setOnlineDevice(online);
        statisticsVo.setOfflineDevice(offline);
        statisticsVo.setDeviceNumb(deviceCount);
        statisticsVo.setMntPerson(mntCount);
        statisticsVo.setRiskNumb(riskCount);
        statisticsVo.setUserNumb(userCount);
        statisticsVo.setDeptNumb(deptCount);
        return statisticsVo;
    }

    @Override
    public List<Object[]> getRiskStatics() {
        User requester = SecurityUtils.getUser();
        Department dept = deptService.findDeptByUserId(requester.getUserId());
        return riskRecordDao.staticsRisk(dept.getDeptId(), 5);
    }

    @Override
    public MonitorDtree getMonitorTree(int rootDeptId) {

        Department rootDept = deptService.findDeptById(rootDeptId);

        MonitorDtree rst = new MonitorDtree();
        rst.setName(rootDept.getName());
        rst.setDeptId(rst.getDeptId());

        List<Department> childDeptList = deptService.findDeptByParentId(rootDept.getDeptId());
        for (Department childDept : childDeptList) {
            // dept device count
            int countInDept = deviceInfoService.findCountInDept(childDept.getDeptId())[0];
            rst.addChildDept(childDept.getName(), countInDept, childDept.getDeptId());
        }

        List<DeviceInfo> childDeviceList = deviceInfoService.findByDeptId(rootDept.getDeptId());
        for (DeviceInfo childDevice : childDeviceList) {
            rst.addChildDevice(childDevice.getDeviceName(), childDevice.getStatus(), childDevice.getDeviceId(),childDevice.getSerialNumber());
        }
        return rst;
    }

    @Override
    public int[] getMonitorStaticsByDevice(int deviceId) {
        DeviceInfo deviceInfo = deviceInfoDao.findById(deviceId).orElse(null);
        assert deviceInfo != null;
        RiskRecord riskRecord = new RiskRecord();

        riskRecord.setDeviceSerial(deviceInfo.getSerialNumber());
        int[] rst = new int[3];

        Example<RiskRecord> example1 = Example.of(riskRecord);
        rst[0] = (int) riskRecordDao.count(example1);
        riskRecord.setState(0);
        Example<RiskRecord> example2 = Example.of(riskRecord);
        rst[1] = (int) riskRecordDao.count(example2);
        riskRecord.setState(null);
        riskRecord.setSerialNumber(String.valueOf(2));
        Example<RiskRecord> example3 = Example.of(riskRecord);
        rst[2] = (int) riskRecordDao.count(example3);
        return rst;
    }
}
